#include "PhaserMunition.h"

#define PI 3.14
static const int DAMAGE = 8;

GLfloat phaserMunition_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat phaserMunition_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat phaserMunition_specular[] = {1.0, 1.0, 1.0, 1.0};

// Shininess
GLfloat phaserMunition_shininess = 100.0f;

PhaserMunition::PhaserMunition(Map *m)
{
	map = m;
	damage = DAMAGE;
	lenght = 1.0;
	radius = 0.03;
	height = 0.1;
	elevation = 0;
	oldX = 0;
	oldZ = 0;
	angle = 0;
	angleAnimation = 0;
	quad = gluNewQuadric();
	loadImage();
}

void PhaserMunition::loadImage()
{
	Image* image = loadBMP("textures\\phaserMunition.bmp");
	textureMunition = loadTexture(image);

	delete image;
}

void PhaserMunition::setAngle(GLfloat a)
{
	if(!animated)
	{
		angle = a;
		angleAnimation = angle;
	}
	else
		angle = a;
}

void PhaserMunition::display()
{
	for (int i=0; i!=2; ++i)
	{
		glPushMatrix();
			glTranslatef(0.0, elevation - radius, 0.0);
			glTranslatef(x, 0, z);
			//glRotatef(180*angleAnimation/PI, 0, 1, 0);
			glRotatef(angleAnimation, 0, 1, 0);

			glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, phaserMunition_diffuse);
			glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, phaserMunition_ambient );
			glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, phaserMunition_specular);
			glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, phaserMunition_shininess);

			glBindTexture(GL_TEXTURE_2D, textureMunition);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			gluQuadricNormals(quad, GLU_SMOOTH);
			gluQuadricTexture(quad, GL_TRUE);
			glTranslatef(pow(-1.0, i)*(0.25), 0, -lenght/2);
			glPushMatrix();
				glRotatef(180, 1, 0, 0);
				gluDisk(quad, 0, radius, 15, 2);
			glPopMatrix();
			glPushMatrix();
				glTranslatef(0, 0, lenght);
				gluDisk(quad, 0, radius, 15, 2);
			glPopMatrix();

			gluCylinder(quad, radius, radius, lenght, 15, 2);
		glPopMatrix();
	}
}

void PhaserMunition::animate()
{
	if(animated)
	{
		oldX += sin(PI*angleAnimation/180);
		oldZ += cos(PI*angleAnimation/180);

		int idEnnemy = map->move(myX + sin(PI*angleAnimation/180), y, myZ + cos(PI*angleAnimation/180));
		if (!idEnnemy)
		{
			x = oldX;
			z = oldZ;
			y = elevation;
			myX += sin(PI*angleAnimation/180);
			myZ += cos(PI*angleAnimation/180);
		}
		else if(idEnnemy < 0)
		{
			reset();
		}
		else
		{
			collisionID = idEnnemy;
			crash = true;
			reset();
		}
	}
}

void PhaserMunition::reset()
{
	oldX = 0;
	oldZ = 0;
	x = 0;
	z = 0;
	myX = xPos;
	myY = yPos;
	myZ = zPos;
	angleAnimation = angle;
	animated = false;
}

PhaserMunition::~PhaserMunition(void)
{
}
